
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:qianyi/entity/home/recommend/Category.dart';
import 'package:qianyi/entity/home/trendVo.dart';
import 'package:qianyi/network/http_entity.dart';
import 'package:qianyi/state/token.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../../entity/home/recommend/Record.dart';
import '../../../network/base_entity.dart';
import '../../../network/http_config.dart';
import '../../../network/http_manager.dart';
import '../../../ui/widget/custom_refresh_mixin.dart';



class RecommendTagViewModel extends ChangeNotifier with CustomRefreshMixin {
  TokenController tokenController = Get.find();
  TabController? tabController;
  Records? r1;
  Records? r2;
  Records? r3;
  Records? r4;
  int page = 1;
  bool isTag = false;
  String? tagId;
  @override
  Future<List> requestList(int page) async {

    Map<String,dynamic> params = {
      "id": tagId,
      "type": 1
    };
    if(r1 == null) {
      HttpEntity result2 = await HttpManager.getInstance().request(url: "/platform/category/getImgListByCategory/${page}/10",method: Method.get,params: params) as HttpEntity;
      r1 = Records.fromJson(result2!.getOriData()['data']['records']);

    } else {
      HttpEntity result2 = await HttpManager.getInstance().request(url: "/platform/category/getImgListByCategory/${page}/10",method: Method.get,params: params) as HttpEntity;
      r2 = Records.fromJson(result2!.getOriData()['data']['records']);
    }
    if(r2!=null && r2!.records!=null) {
      if(page == 1) {
        notifyListeners();
        r1!.records!.addAll(r2!.records!);

      } else {
        notifyListeners();
        return r2!.records!;
      }

    }

    notifyListeners();
    return r1!.records!;
  }

  Future<void> switchTags(String? id) async {
    tagId = id;
    isTag = true;
    r1 = null;
    r2 = null;
    this.page = 1;
    await this.requestList(page);
    this.page++;
  }

  Future<void> requestTagsModel(String? id, int page) async {
    Map<String,dynamic> params = {
      "id": id,
      "type": 1
    };
    if(r1 == null) {
      HttpEntity result2 = await HttpManager.getInstance().request(url: "/platform/category/getImgListByCategory/1/10",method: Method.get,params: params) as HttpEntity;
      r1 = Records.fromJson(result2!.getOriData()['data']['records']);

    } else {
      HttpEntity result2 = await HttpManager.getInstance().request(url: "/platform/category/getImgListByCategory/1/10",method: Method.get,params: params) as HttpEntity;
      r2 = Records.fromJson(result2!.getOriData()['data']['records']);
    }
    if(r2!=null && r2!.records!=null) {
      r1!.records!.addAll(r2!.records!);

    }
    this.datas = r1!.records!;
    notifyListeners();
  }

  Future<void> onLoadingTag() async {
    Map<String,dynamic> params = {
      "id": tagId,
      "type": 1
    };
    if(r1 == null) {
      HttpEntity result2 = await HttpManager.getInstance().request(url: "/platform/category/getImgListByCategory/${this.page}/10",method: Method.get,params: params) as HttpEntity;
      r1 = Records.fromJson(result2!.getOriData()['data']['records']);

    } else {
      HttpEntity result2 = await HttpManager.getInstance().request(url: "/platform/category/getImgListByCategory/${this.page}/10",method: Method.get,params: params) as HttpEntity;
      r2 = Records.fromJson(result2!.getOriData()['data']['records']);
    }
    if(r2!=null && r2!.records!=null) {
      r1!.records!.addAll(r2!.records!);

    }
    this.datas = r1!.records!;
    this.page++;
    notifyListeners();

  }


}
